﻿@page "/T客商门店对照"
@using System.Diagnostics.CodeAnalysis
@using BootstrapBlazor.OnlyServerZhongShun.Data.dbmodels
@using System.Data
@inject AuthenticationStateProvider authenticationStateProvider
@inject ToastService ToastService
@inject IConfiguration _congifuration
@inject SqlSugarScope _SqlSugarScope
@attribute [Authorize(Roles = "超级管理员")]


<Table TItem="T客商门店对照"
       IsPagination="true" PageItemsSource="@PageItemsSource"
       IsStriped="true" IsBordered="true"
       ShowDefaultButtons="true"
       ShowToolbar="true" ShowDeleteButton="true"
       ClickToSelect="true" DoubleClickToEdit="true" ShowRefresh="false" ShowExportButton="true"
       OnQueryAsync="@OnQueryAsync" OnAddAsync="@OnAddAsync" OnDeleteAsync="@onDelete" OnSaveAsync="@OnSaveAsync">
    <TableColumns>
        <TableColumn @bind-Field="@context.U8客户编码" Width="180" />
        <TableColumn @bind-Field="@context.U8name" Width="100" />
        <TableColumn @bind-Field="@context.posName" />
    </TableColumns>
    <EditTemplate>
        <div class="row g-3">
            <div class="col-12 col-sm-6">
                <BootstrapInput @bind-Value="@context.U8客户编码" placeholder="不可为空" maxlength="500">
                </BootstrapInput>
            </div>
            <div class="col-12 col-sm-6">
                <BootstrapInput @bind-Value="@context.U8name" placeholder="不可为空" maxlength="500">
                </BootstrapInput>
            </div>
            <div class="col-12 col-sm-6">
                <BootstrapInput @bind-Value="@context.posName" placeholder="不可为空" maxlength="500">
                </BootstrapInput>
            </div>
        </div>
    </EditTemplate>
</Table>
@code {
    private static IEnumerable<int> PageItemsSource => new int[] { 10, 20, 40, 100, 1000 };
    [NotNull]
    private List<Foo>? Items { get; set; }

    private Task<QueryData<T客商门店对照>> OnQueryAsync(QueryPageOptions options)
    {
        IEnumerable<T客商门店对照> items = _SqlSugarScope.Queryable<T客商门店对照>().OrderBy(it =>it.U8客户编码).ToList();

        // 设置记录总数
        var total = items.Count();

        // 内存分页
        items = items.Skip((options.PageIndex - 1) * options.PageItems).Take(options.PageItems).ToList();

        return Task.FromResult(new QueryData<T客商门店对照>()
            {
                Items = items,
                TotalCount = total,
                IsSorted = true,
                IsFiltered = true,
                IsSearch = true
            });
    }

    private static Task<T客商门店对照> OnAddAsync() => Task.FromResult(new T客商门店对照() { guid = Guid.NewGuid()});

    private Task<bool> OnSaveAsync(T客商门店对照 item, ItemChangedType changedType)
    {
        // 增加数据演示代码
        if (changedType == ItemChangedType.Add)
        {
            _SqlSugarScope.Insertable(item).ExecuteCommand();
        }
        else
        {
            _SqlSugarScope.Updateable(item).WhereColumns(it => it.guid).ExecuteCommand();
        }
        return Task.FromResult(true);
    }

    private Task<bool> onDelete(IEnumerable<T客商门店对照> item)
    {
        // 增加数据演示代码
        _SqlSugarScope.Deleteable<T客商门店对照>(item).ExecuteCommand();
  
        return Task.FromResult(true);
    }
}
